package library.entity;

import java.util.Calendar;
import java.util.Date;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;


@DatabaseTable(tableName = "booking_record")
public class BookingRecord {
	@DatabaseField(generatedId = true)
	private int id;
	@DatabaseField(canBeNull = false)
	private Date returnDate;
    @DatabaseField
    private short renewCount;
	@DatabaseField(foreign = true, columnName = "res_id", foreignAutoRefresh = true)
	private Book book;
    @DatabaseField(foreign = true, columnName = "user_id", foreignAutoRefresh = true)
	private Reader user;
	
	protected BookingRecord(){

	}
	
	public Calendar getReturnDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(returnDate);
		return calendar;
	}

	public void setReturnDate(Calendar date) {
		this.returnDate = date.getTime();
	}

    public String getReturnDateInString() {
        Calendar date = getReturnDate();
        String strDate = String.format("%d/%d/%d",date.get(Calendar.YEAR),date.get(Calendar.MONTH)+1,date.get(Calendar.DAY_OF_MONTH));
        return strDate;
    }
	
	public BookingRecord(Book borrowBook, Calendar date, Reader bookingUser){
		book = borrowBook;
		returnDate = date.getTime();
		user = bookingUser;
	}

    public Book getBook() {return this.book;}

    public String getBookName(){
        return book.getName();
    }

    public int getBookID(){
        return book.getId();
    }

    public int getRenewCount(){
        return renewCount;
    }

    public void updateRenewCount(){
        renewCount++;
    }
}
